package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.AuditStatusEnum;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lww
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertificationAuditService {

    @Resource
    IWorkerCertificationService workerCertificationService;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> queryPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        //1.构建查询条件
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(dto.getName()), WorkerCertificationAudit::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo())
                .eq(Objects.nonNull(dto.getCertificationStatus()), WorkerCertificationAudit::getCertificationStatus, dto.getAuditStatus())
                .eq(Objects.nonNull(dto.getAuditStatus()), WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus());


        //2.分页查询
        PageResult<WorkerCertificationAuditResDTO> workerCertificationAuditResDTOPageResult = PageHelperUtils.selectPage(dto, () -> {
            List<WorkerCertificationAudit> workerCertificationAudits = baseMapper.selectList(wrapper);
            //2.1转换类型
            return workerCertificationAudits.stream().map((audit) -> BeanUtil.toBeanIgnoreError(audit, WorkerCertificationAuditResDTO.class))
                    .collect(Collectors.toList());
    });


        return workerCertificationAuditResDTOPageResult;
}
    /**
     * 审核服务人员的认证信息
     * @param dto 认证信息审核请求体
     * @param id 认证信息id
     */
    @Override
    public void audit(CertificationAuditReqDTO dto, Long id) {
        LambdaUpdateWrapper<WorkerCertificationAudit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WorkerCertificationAudit::getId,id)
                        .set(WorkerCertificationAudit::getCertificationStatus,dto.getCertificationStatus())
                        .set(StrUtil.isNotBlank(dto.getRejectReason()),WorkerCertificationAudit::getRejectReason,dto.getRejectReason())
                         //审核人和审核时间
                        .set(WorkerCertificationAudit::getAuditorName,UserContext.currentUser().getName())
                        .set(WorkerCertificationAudit::getAuditorId,UserContext.currentUserId())
                        .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now())
                        //审核状态
                        .set(WorkerCertificationAudit::getAuditStatus, AuditStatusEnum.AUDITED.getCode());

        baseMapper.update(null, wrapper);
        //修改服务人员认证信息表的认证状态
        WorkerCertificationAudit audit = baseMapper.selectById(id);
        WorkerCertification workerCertification = new WorkerCertification();
        workerCertification.setCertificationStatus(dto.getCertificationStatus())
                .setId(audit.getServeProviderId());
        workerCertificationService.updateById(workerCertification);

    }
}
